/**  
* Title: AbstractBaseService.java 
* Description:  
* Copyright: Copyright (c) 2017  
* Company: www.baidudu.com 
* @author xuchang  
* @date 2018年7月25日  
* @version 1.0  
*/  
package com.bclz.common.base;


import java.lang.reflect.Field;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**  
* Title: AbstractBaseService
* Description: 基础service类
* @author xuchang 
* @date 2018年7月25日  
*/
public abstract class AbstractBaseService<T ,M extends BaseMapper<T>> implements IBaseService<T>  {
	
	@Autowired
	protected M mapper;
	
	
	public PageInfo<T> findbyField(T entity) throws Exception {
		Field i=entity.getClass().getSuperclass().getDeclaredField("pageNum");
		i.setAccessible(true);
		int pageNum=i.getInt(entity);
		i=entity.getClass().getSuperclass().getDeclaredField("pageSize");
		i.setAccessible(true);
		int pageSize=i.getInt(entity);
		Page<T> page=PageHelper.startPage(pageNum, pageSize, true);
		mapper.listbyField(entity);
		PageInfo<T> info=new PageInfo<T>(page);
		return info;
	}
	
	@Transactional
	public int add(T entity) throws Exception{
		return mapper.save(entity);
	};
	
	@Override
	public T getbyId(String id) {
		// TODO Auto-generated method stub
		return mapper.getbyId(id);
	}

	@Override
	@Transactional
	public int deleteById(List<String> ids) {
		// TODO Auto-generated method stub
		return mapper.deleteById(ids);
	}

	@Override
	public List<T> listAll() {
		// TODO Auto-generated method stub
		return mapper.listAll();
	}

	@Transactional
	public int update(T entity) throws Exception{
		
		return mapper.update(entity);
		
	}
	
	
}
